package huaban;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 抓取花瓣图片
 */
public class Bg {

    public static void main(String[] args) throws IOException {

        ExecutorService pool = Executors.newFixedThreadPool(1);// 创建一个固定大小为5的线程池

        Map<String, String> headers = new HashMap<>();
        headers.put("X-Request", "JSON");
        headers.put("Host", "huaban.com");
        headers.put("Referer", "http://huaban.com/favorite/beauty/");
        headers.put("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
        headers.put("X-Requested-With", "XMLHttpRequest");
        headers.put("Accept", "application/json");
        int max = 1155196366;
        int limit = 100;
        String url = "http://huaban.com/favorite/beauty/?j2oofuyl&max=" + max + "&limit=" + limit + "&wfl=1";

        for(int i=0;i<10;i++){
            String result = Jsoup.connect(url).headers(headers).ignoreContentType(true).execute().body();
            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONArray jsonArray = (JSONArray) jsonObject.get("pins");
            for (Object object : jsonArray) {
                JSONObject file = (JSONObject)((JSONObject)object).get("file");
                String key = (String)file.get("key");
                Download download = new Download();
                download.setKey(key);
                pool.submit(download);
            }
            max=max+limit;
        }

        pool.shutdown();
    }


}
